package com.app.newiptv.activity;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.app.newiptv.R;
import com.app.newiptv.adaper.PlayDetailAdapter;
import com.app.newiptv.base.MyApplication;
import com.app.newiptv.bean.ProgramBean;
import com.app.newiptv.helper.MySQLiteOpenHelper;
import com.app.newiptv.presenter.PlayDetailsPresenter;
import com.app.newiptv.utils.APPLog;
import com.app.newiptv.utils.Paths;
import com.bumptech.glide.Glide;

import static com.app.newiptv.utils.Paths.BASEPATH;


public class FilmDetailsActivity extends Activity implements View.OnClickListener, View.OnFocusChangeListener {
    private RecyclerView mRecommend_rv;
    private PlayDetailsPresenter playDetailsPresenter;
    private Button mPlay_btn;
    private Button mCollection_btn;
    private ImageView focusView;
    private int id;
    private ImageView mImg_view;
    private TextView mFilm_name;
    private TextView mDirector_tv;
    private TextView to_star_tv;
    private TextView years_tv;
    private TextView introduce_tv;
    private TextView clear_type;
    private TextView copyright_text;
    private TextView text_groom;
    private SQLiteDatabase readDatabase;
    private SQLiteDatabase writeDatabase;
    private MySQLiteOpenHelper sqlHelper;
    private ProgramBean.DataBean.ListBean listBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_play_details);
        Bundle bundle = getIntent().getBundleExtra("bundle");
        id = bundle.getInt("id", 1);
        sqlHelper = MySQLiteOpenHelper.getInstance(this);
        readDatabase = sqlHelper.getReadInstance();
        writeDatabase = sqlHelper.getWriteInstance();
        initView();
        initData();
        setLinsenter();
    }

    private void initView() {
        mRecommend_rv = (RecyclerView) findViewById(R.id.recommend_rv);
        mPlay_btn = (Button) findViewById(R.id.play_btn);
        mCollection_btn = (Button) findViewById(R.id.collection_btn);
        focusView = (ImageView) findViewById(R.id.focus_view);

        mImg_view = (ImageView) findViewById(R.id.img_view);
        mFilm_name = (TextView) findViewById(R.id.film_name);
        mDirector_tv = (TextView) findViewById(R.id.director_tv);
        to_star_tv = (TextView) findViewById(R.id.to_star_tv);
        years_tv = (TextView) findViewById(R.id.years_tv);
        introduce_tv = (TextView) findViewById(R.id.introduce_tv);
        clear_type = (TextView) findViewById(R.id.clear_type);
        copyright_text = (TextView) findViewById(R.id.copyright_text);
        text_groom = (TextView) findViewById(R.id.text_groom);
    }

    private void initData() {

        clear_type.setTypeface(MyApplication.face);
        copyright_text.setTypeface(MyApplication.face);
        text_groom.setTypeface(MyApplication.face);
        playDetailsPresenter = new PlayDetailsPresenter(this);
        playDetailsPresenter.setDisplayed(true);
        String url = Paths.HOSTPATH + Paths.CHANNLE_DETAIL + "?ids=" + id;
        String hotUrl = Paths.HOSTPATH + Paths.CHANNLE_LIST + "?t=100000";
        APPLog.printInfo("hotUrl==>:" + hotUrl);
        playDetailsPresenter.loadDetailContent(url, detailCallback);
        playDetailsPresenter.loadListItem(hotUrl, detailCallback);
        mCollection_btn.setNextFocusDownId(R.id.recommend_rv);
        mCollection_btn.setNextFocusRightId(R.id.collection_btn);
        mPlay_btn.setNextFocusDownId(R.id.recommend_rv);
        mPlay_btn.setNextFocusLeftId(R.id.play_btn);
    }

    private void setLinsenter() {
        mPlay_btn.setOnClickListener(this);
        mCollection_btn.setOnClickListener(this);
        mPlay_btn.setOnFocusChangeListener(this);
        mCollection_btn.setOnFocusChangeListener(this);
    }

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }

    private PlayDetailsPresenter.Callback detailCallback = new PlayDetailsPresenter.Callback() {
        @Override
        public void onError(int code, String msg) {

        }

        @Override
        public void onMovieCompleted(PlayDetailsPresenter presenter, int count) {
            PlayDetailAdapter playDetailAdapter = new PlayDetailAdapter(presenter, focusView, FilmDetailsActivity.this);
            playDetailAdapter.setCount(count);
            LinearLayoutManager manager = new LinearLayoutManager(FilmDetailsActivity.this);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            mRecommend_rv.setLayoutManager(manager);
            mRecommend_rv.setAdapter(playDetailAdapter);
            mRecommend_rv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        if (mRecommend_rv.getChildCount() > 0) {
                            mRecommend_rv.getChildAt(0).requestFocus();
                        }
                    }
                }
            });
        }

        @Override
        public void onDetailCompleted(PlayDetailsPresenter presenter) {
            presenter.onBindDetailData(new OnBindDetail());
            listBean = playDetailsPresenter.detailsBean.getList().get(0);
            Cursor cursor = readDatabase.query("RecodeCollect", null, "name=" + "'" + listBean.getVod_name() + "'"+" and " + "type=1", null, null, null, null);
            mCollection_btn.setVisibility(cursor.moveToFirst()?View.INVISIBLE:View.VISIBLE);
            cursor.close();
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.play_btn:
                if (Contast.USE_GROUP.equals("3")) {
                    playDetailsPresenter.toPlay();
                } else {
                    Toast.makeText(FilmDetailsActivity.this, "您不是VIP", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.collection_btn:
                ContentValues values = new ContentValues();
                values.put("pic_url", listBean.getVod_pic());
                values.put("name", listBean.getVod_name());
                values.put("actor", listBean.getVod_actor());
                values.put("type", 1);
                values.put("collect_time", System.currentTimeMillis());
                values.put("movie_id", listBean.getVod_id());
                values.put("movie_type", 1001);
                Log.i("legendki", "onClick: " + values);
                Log.i("legendki", "writeDatabase: " + writeDatabase);
                if (!writeDatabase.isOpen()) {
                    writeDatabase = sqlHelper.getWriteInstance();
                }
                writeDatabase.insert("RecodeCollect",null, values);
                mCollection_btn.setVisibility(View.INVISIBLE);
                break;
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()) {
            case R.id.play_btn:
                if (hasFocus) {
                    mPlay_btn.setBackgroundResource(R.mipmap.pd_play_focused);
                } else {
                    mPlay_btn.setBackgroundResource(R.mipmap.pd_play_normal);
                }
                break;
            case R.id.collection_btn:
                if (hasFocus) {
                    mCollection_btn.setBackgroundResource(R.mipmap.pd_collect_focused);
                } else {
                    mCollection_btn.setBackgroundResource(R.mipmap.pd_collect_normal);
                }
                break;
        }
    }

    private class OnBindDetail implements PlayDetailsPresenter.IPlayDetailsViewHolder {

        @Override
        public void setImg_view(String url) {
            Log.d("tag", "-----------url---------" + url);
            Glide.with(FilmDetailsActivity.this).load(BASEPATH + url).error(R.mipmap.iron).into(mImg_view);
        }

        @Override
        public void setFilm_name(String text) {
            mFilm_name.setTypeface(MyApplication.face);
            mFilm_name.setText(text);
        }

        @Override
        public void setDirector_tv(String text) {
            mDirector_tv.setTypeface(MyApplication.face);
            mDirector_tv.setText(text);
        }

        @Override
        public void setTostar_tv(String text) {
            to_star_tv.setTypeface(MyApplication.face);
            to_star_tv.setText(text);
        }

        @Override
        public void setYears_tv(String text) {
            years_tv.setTypeface(MyApplication.face);
            years_tv.setText(text);
        }

        @Override
        public void setIntroduce_tv(String text) {
            introduce_tv.setTypeface(MyApplication.face);
            introduce_tv.setText(text);
        }

        @Override
        public void setCopyright(String copyright) {
            copyright_text.setTypeface(MyApplication.face);
            copyright_text.setText("版权来源：" + copyright);
        }
    }
}
